package org.sabayframework.cluster.impl.felix;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.sabayframework.cluster.ClusteringService;
import org.sabayframework.cluster.impl.ClusteringServiceImpl;
import org.sabayframework.log.Log;


/**
 * The activator of the host application bundle. 
**/
public class Activator implements BundleActivator
{
	private BundleTracker tracker;
	private ClusteringServiceImpl service;
    /**
     * Starts bundle tracking;
     * @param context The context of the bundle.
    **/
    public void start(final BundleContext context) throws Exception
    {
    	tracker= new BundleTracker(context);
    	tracker.open();
    	ClusteringServiceImpl service = new ClusteringServiceImpl();
    	service.start();
    	Hashtable<String, String> props = new Hashtable<String, String>();
    	props.put("port", String.valueOf(service.getPort()));
        context.registerService(ClusteringService.class.getName(), service, props);
    	this.service = service;

    	Log.info("Activator started");
    }

    /**
     * Stops service tracking.
     * @param context The context of the bundle.
    **/
    public void stop(BundleContext context)
    {
    	tracker.close();
    	if(service!=null){
    		service.stop();
    		service = null;
    	}
    	Log.info("Activator stopped");
    }

}